<?php

/**
 *webpos 销售统计
*/

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');
if ($_REQUEST['act'] == 'list') {
     $order_count = $GLOBALS['db']->getOne(' SELECT COUNT( DISTINCT ywid ) FROM '.$GLOBALS['ecs']->table('order_info').' WHERE pay_status = 2 AND is_webpos = 1 ');

    if (!isset($_REQUEST['start_date']))
    {
        $start_date = local_strtotime('-7 days');
    }
    if (!isset($_REQUEST['end_date']))
    {
        $end_date = local_strtotime('today');
    }

    $order_report = get_sale_report();

    /* 赋值到模板 */
    $smarty->assign('filter',       $order_report['filter']);
    $smarty->assign('record_count', $order_report['record_count']);
    $smarty->assign('page_count',   $order_report['page_count']);
    $smarty->assign('goods_sales_list', $order_report['sale_list_data']);
    $smarty->assign('ur_here',          '销售报告');
    $smarty->assign('full_page',        1);
    $smarty->assign('start_date',       local_date('Y-m-d', $start_date));
    $smarty->assign('end_date',         local_date('Y-m-d', $end_date));
    
    $smarty->display('sale_report.htm');
}
if ($_REQUEST['act'] == 'query') {

    if (strstr($_REQUEST['start_date'], '-') === false)
    {
        $_REQUEST['start_date'] = local_date('Y-m-d', $_REQUEST['start_date']);
        $_REQUEST['end_date'] = local_date('Y-m-d', $_REQUEST['end_date']);
    }
    $order_report = get_sale_report();

    /* 赋值到模板 */
    $smarty->assign('filter',       $order_report['filter']);
    $smarty->assign('record_count', $order_report['record_count']);
    $smarty->assign('page_count',   $order_report['page_count']);
    $smarty->assign('goods_sales_list', $order_report['sale_list_data']);
    make_json_result($smarty->fetch('sale_report.htm'), '', array('filter' => $order_report['filter'], 'page_count' => $order_report['page_count']));
}

//整合销售报告
function get_sale_report($is_pagination = true)
{
    //时间参数
    $filter['start_date'] = empty($_REQUEST['start_date']) ? local_strtotime('-7 days') : local_strtotime($_REQUEST['start_date']);
    $filter['end_date'] = empty($_REQUEST['end_date']) ? local_strtotime('today') : local_strtotime($_REQUEST['end_date']);
    $where = ' WHERE pay_status = 2 AND is_webpos = 1 AND add_time > '.$filter['start_date'].' AND add_time < '.$filter['end_date'].' ';

    $order_count = $GLOBALS['db']->getOne(' SELECT COUNT( DISTINCT ywid) FROM '.$GLOBALS['ecs']->table('order_info').$where.' AND pay_status = 2 AND is_webpos = 1 ');
    $filter['record_count'] = $order_count;

    /* 分页大小 */
    $filter = page_and_size($filter);
    $sql = 'SELECT money_paid, ywid FROM '.$GLOBALS['ecs']->table('order_info').$where.' AND pay_status = 2 AND is_webpos = 1 ORDER BY ywid ASC ';
    if ($is_pagination)
    {
        $sql .= " LIMIT " . $filter['start'] . ', ' . $filter['page_size'];
    }
    $order_info = $GLOBALS['db']->getAll($sql);

    $order_report = array();
    if ($order_info) {
        $temp_ywid = 0;
        $pos = 0;
        foreach ($order_info as $key => $value) {
            $temp_ywid = $value['ywid'];
            if ($order_report[$pos]['ywid'] != $temp_ywid) {
                $pos++;
            }
            if (!$order_report[$pos]['amount']) {
                $order_report[$pos]['amount'] = 0;
            }
            $order_report[$pos]['ywid'] = $temp_ywid;
            $order_report[$pos]['yw_name']  = $GLOBALS['db']->getOne(' SELECT user_name FROM '.$GLOBALS['ecs']->table('users').' WHERE user_id = "'.$temp_ywid.'"');
            $order_report[$pos]['amount'] += $value['money_paid'];

        }
    }
    $arr = array('sale_list_data' => $order_report, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);

    return $arr;
}
?>